/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.api.internal;

import net.sourceforge.perforce.api.IClientApi;
import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.api.PerforceApiPlugin;
import net.sourceforge.perforce.api.Policy;

/**
  * Wrapper class for Perforce API ClientApi class.
  * 
  * @version $Revision: 67 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net>Boris Pruessmann</a>
  */
public final class ClientApi implements IClientApi
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Used to store the native reference.
	  * <br>DON'T TOUCH THE CONTENT OF THIS VARIABLE 
	  */
	private int _nativeHandle;

	/** Stores an instance of Error used duting init and dispose. */
	private Error error;

	// ------------------------------------------------------------------------- Public Constructors

	/** Constructor for ClientApi. */
	public ClientApi()
	{
		construct();
	}

	// ------------------------------------------------------------------------------ Public Methods

	/** @see IClientApi#init() */
	public void init()
	{
		error = new Error();
		init(error);
	}


	/** @see IClientApi#dispose() */
	public int dispose()
	{
		int result = dispose(error);
		destruct();
		
		error.dispose();
		error = null;
		
		return result;
	}

	/** @see IClientApi#run(IPerforceCommand) */
	public synchronized void run(String[] arguments, IPerforceCommand delegate)
	{
		long start = System.currentTimeMillis();
		
		ClientUser clientUser = new ClientUser(delegate);
		try
		{	
			setArguments(arguments);
			run(delegate.getCommand(), clientUser);
		}
		finally
		{
			clientUser.dispose();
		}	
		
		long stop = System.currentTimeMillis();
		if (PerforceApiPlugin.getPlugin().isDebugging())
		{
			Object[] values = new Object[]
			{ 
				delegate.getCommand(), 
				new Long(stop - start) 
			} ;
			
			StringBuffer message = new StringBuffer(Policy.bind("ClientApi.run", values));
			if (arguments.length > 0)
			{
				message.append(" [Arguments: ");
				for (int i = 0; i < arguments.length; i++)
				{
					message.append(arguments[i].toString());
					if (i < arguments.length - 1)
					{
						message.append(", ");
					}
				}
				message.append("]");
			}
			
			System.out.println(message.toString()); //$NON-NLS-1$
		}
	}

	/** @see IClientApi#dropped() */
	public native boolean dropped();
		
	/** @see IClientApi#setPerforcePort(String) */
	public native void setPort(String serverNameAndPort);

	/** @see IClientApi#getPerforcePort() */
	public native String getPort();

	/** @see IClientApi#setPerforceClient(String) */
	public native void setClient(String client);

	/** @see IClientApi#getPerforceClient() */	
	public native String getClient();

	/** @see IClientApi#setPerforceUser(String) */
	public native void setUser(String user);

	/** @see IClientApi#getPerforceUser() */
	public native String getUser();
		
	/** @see IClientApi#setProtocol(String, String) */
	public native void setProtocol(String key, String value);

	/** @see IClientApi#getCharset() */
	public native String getCharset();

	/** @see IClientApi#setCharset(String) */
	public native void setCharset(String charset);

	/** @see IClientApi#getHost() */
	public native String getHost();
	
	/** @see IClientApi#setHost(String) */
	public native void setHost(String host);

	/** @see IClientApi#getLanguage() */
	public native String getLanguage();
	
	/** @see IClientApi#setLanguage(String) */
	public native void setLanguage(String language);

	/** @see IClientApi#getPassword() */
	public native String getPassword();

	/** @see IClientApi#setPassword(String) */
	public native void setPassword(String password);

	/** @see IClientApi#getProtocol(String) */
	public native String getProtocol(String key);

	/** @see IClientApi#getPerforceError() */
	public Error getPerforceError()
	{
		return error;
	}
	
	// ----------------------------------------------------------------------------- Private Methods

	/** Wrapper for ClientApi::Init.
	  * 
	  * @param error instance of Error used for error submission.
	  */
	private native void init(Error error);

	/** Wrapper for ClientApi::SetArgv.
	  * 
	  * @param arguments arguments for the next command to execute.
	  */
	private native void setArguments(String[] arguments);
	
	/** Wrapper for ClientApi::Run.
	  * 
	  * @param function name of the perforce command.
	  * @param clientUser IClientUser instance used for interaction.
	  */
	private native void run(String function, ClientUser clientUser);
	
	/** Wrapper for ClientApi::Final.
	  * 
	  * @param error instance of Error used for error submission.
	  * @return result code.
	  */
	private native int dispose(Error error);

	/**
	  * Called by the constructor to initialize the native side
	  * of this class.
	  */
	private native void construct();
	
	/**
	  * Called by dispose() to destroy the native side of this
	  * class.
	  */
	private native void destruct();
}

